<?php

/**
 * --------------------------------
 * Default CheckBookRss
 * 
 * 队列
 * 拾光 每日轮询检查用户超期图书的接口  CheckBookRss
 * 
 * @package 	laravel
 * @subpackage	app/controllers
 * @version 	1.0.0
 * --------------------------------
 */

class CheckBookRss extends BaseController {

	function app($job , $data){
		extract($data);
		$insert_arr = array(
			'user_id' 	=> $user_id,
			'rss' 		=> $rss
			);
		if( $rss > 0 ){
			$already_over_count 	= $this->readRss($rss , 3);
			$five_over_count 		= $this->readRss($rss , 4);
			$insert_arr['five']		= $five_over_count;
			$insert_arr['alreay']	= $already_over_count;
			DB::table('queue_rec')->insert($insert_arr);
			$title = "";
			$reply_opts = [
					['content'=>'知道了','is_expected'=>true]
				];
			if( $already_over_count  == 0 && $five_over_count > 0 ){
				$title = "你有".$five_over_count."本书即将超期，请按时归还或登录拾光还书提醒续借";
				$reply_opts = [
					['content'=>'知道了'],
					['content'=>'还书','url'=>'http://timepicker.cn:91','is_expected'=>true]
				];
			}
			if( $already_over_count > 0 && $five_over_count == 0 ){
				$title = "你有".$already_over_count."本书已经超期，请及时到图书馆归还超期图书";
			}
			if( $already_over_count > 0 && $five_over_count > 0 ){
				$title = "你有".$already_over_count."本书已经超期，有".$five_over_count."本书即将到期，请及时到图书馆归还超期图书";
			}
			if( $title != "" ){
				$inform_arr = array(
					'title' 		=> $title,
					'app_id' 		=> 8,
					'set_id' 		=> 8,
					'sender_id' 	=> 1,
					'users' 		=> [['user_id'=>$user_id]],
					'reply_opts' 	=> $reply_opts
					);
				//print_r($inform_arr);
				$this->curl_post( "http://timepicker.cn/api/inform/send" , $inform_arr );
			}
		}else{
			DB::table('queue_rec')->insert($insert_arr);
		}
	}


	function readRss($rss_id , $type){
		//直接请求rss接口
		$rss_url = "http://202.206.242.99/reader/rss.php?type=".$type."&id=".$rss_id;
		//设置编码为UTF-8    
		$fp = fopen($rss_url , "r") or die("can not open $rssfeed");
		$buff = "";
		while ( !feof($fp) ) {    
	        $buff .= fgets($fp,4096);    
	    }    
	    //关闭文件打开    
	    fclose($fp);  
	    //建立一个 XML 解析器    
		$parser = xml_parser_create();
		//xml_parser_set_option -- 为指定 XML 解析进行选项设置    
	    xml_parser_set_option($parser , XML_OPTION_SKIP_WHITE , 1);    
	    //xml_parse_into_struct -- 将 XML 数据解析到数组$values中    
	    xml_parse_into_struct($parser , $buff , $values , $idx);    
	    //xml_parser_free -- 释放指定的 XML 解析器    
	    xml_parser_free($parser);
	    //header("Content-type:text/html;charset=utf-8");
	    //$values = mb_convert_encoding($values , "UTF-8//IGNORE" , "GB2312");
	    $is_item = -1;
	    $temp_counter = 0;
	    foreach( $values as $val ){
	    	$tag = $val['tag'];
	    	$type = $val['type'];
	    	//$value = $val['value'];
	    	//标签统一转为小写
    		$tag = strtolower($tag);
    		if( $tag == 'item' && $type == 'open' ){
    			$is_item = 1;
    		}else if( $tag == 'item' && $type == 'close' ){
    			$is_item = 0;
    		}
    		if( $is_item == 1 ){
    			if( $tag == 'title' ){
    				$temp_counter++; 
    			}
    		}
	    }
	    return $temp_counter;
	}

}



